## To run an R script and save logs appropriately
R = Rscript --verbose --vanilla $(1) > $(1:.r=.log) 2>&1

## Shorthand for shapefile trio
mpio = mpio.shp mpio.dbf mpio.shx

all :	table1-and-a1.log \
		table2.log \
		table3.log \
		figure1.pdf \
		figure2.pdf \
		tablea2.log \
		tablea3.log \
		figurea1-a.png figurea1-b.png \
		figurea2-a.png figurea2-b.png \
		tablea4-single-entrant.log \
		tablea4-entry25.log \
		tablea4-entry75.log \
		tablea4-threshold.log \
		tablea4-first-stage.log \
		tablea5-state-effects.log \
		tablea5-intensity-control.log \
		tablea5-interaction.log \
		tablea6.log \
		tablea7.log \
		figurea3.pdf \
		figurea4.pdf

table1-and-a1.log : violence_t_export.dta table1-and-a1.do
	stata -b table1-and-a1.do

table2.log boot_res_region_v.dta : violence_t_export.dta table2.do
	stata -b table2.do

phats.csv : violence_t_export.dta table3-prep.do
	stata -b table3-prep.do

table3.log : phats.csv violence_t_export.dta boot_res_region_v.dta table3.r
	$(call R, table3.r)

tablea2.log boot_res_region_t.dta : violence_t_export.dta tablea2.do
	stata -b tablea2.do

figure1.pdf : boot_res_region_t.dta figure1.r
	$(call R, figure1.r)

tablea3.log boot_res_elnb.dta : violence_t_export.dta tablea3.do
	stata -b tablea3.do

figure2.pdf : boot_res_elnb.dta figure2.r
	$(call R, figure2.r)

figurea1-a.png figurea1-b.png : violence_t_export.dta code_muni_cede.dta $(mpio) figurea1.r
	$(call R, figurea1.r)

figurea2-a.png : code_muni_cede.dta military_bases.csv colombia_latlong.csv $(mpio) figurea2-a.r
	$(call R, figurea2-a.r)

figurea2-b.png : code_muni_cede.dta group_origins.csv $(mpio) figurea2-b.r
	$(call R, figurea2-b.r)

tablea4-single-entrant.log : violence_t_export.dta phats.csv tablea4-single-entrant.r
	$(call R, tablea4-single-entrant.r)

tablea4-entry25.log : violence_t_export.dta tablea4-entry25.do
	stata -b tablea4-entry25.do

tablea4-entry75.log : violence_t_export.dta tablea4-entry75.do
	stata -b tablea4-entry75.do

tablea4-threshold.log : violence_t_export.dta tablea4-threshold.do
	stata -b tablea4-threshold.do

results-loo-first-stage.csv : violence_t_export.dta tablea4-first-stage-prep.r
	$(call R, tablea4-first-stage-prep.r)

tablea4-first-stage.log : violence_t_export.dta results-loo-first-stage.csv tablea4-first-stage.r
	$(call R, tablea4-first-stage.r)

tablea5-state-effects.log boot_res_deptod.dta : violence_t_export.dta tablea5-state-effects.do
	stata -b tablea5-state-effects.do

tablea5-intensity-control.log boot_res_region_v_inten.dta : violence_t_export.dta tablea5-intensity-control.do
	stata -b tablea5-intensity-control.do

tablea5-interaction.log boot_res_region_inter.dta : violence_t_export.dta tablea5-interaction.do
	stata -b tablea5-interaction.do

tablea6.log : violence_t_export.dta tablea6.do
	stata -b tablea6.do

tablea7.log boot_res_region_v_year.dta : violence_t_year_export.dta tablea7.do
	stata -b tablea7.do

boot_res_region_t_year.dta : violence_t_year_export.dta figurea3-prep.do
	stata -b figurea3-prep.do

figurea3.pdf : boot_res_region_t_year.dta figurea3.r
	$(call R, figurea3.r)

boot_res_eln_year.dta : violence_t_year_export.dta figurea4-prep.do
	stata -b figurea4-prep.do

figurea4.pdf : boot_res_eln_year.dta figurea4.r
	$(call R, figurea4.r)

README.pdf : README.txt
	pandoc README.txt -o README.pdf

replication-final.zip : README.pdf
	zip replication-final.zip *.r *.do colombia_latlong.csv code_muni_cede.dta \
		group_origins.csv military_bases.csv mpio.shp mpio.dbf mpio.shx \
		violence_t_export.dta violence_t_year_export.dta README.* Makefile


.PHONY : clean
clean :
	rm -rf *.log
	rm -rf boot_*.dta
	rm -rf phats.csv
